// pages/mys/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    jscode: '', //用户授权CODE
    // avatarUrl: '',
    // nickname: '',
    isPhone: false, //用户手机号
    isUser: false, //用户弹窗，false显示，true隐藏
    isPop: 1, //分类弹窗：0显示，1不显示
    userPhone: '', 
    userInfo: {}, //用户信息
    catelist: [
    //   {id: 0, title: '所有'},
    //   {id: 1, title: '个人'},
    //   {id: 2, title: '门店'},
    //   {id: 3, title: '商品'}
    ],
    cateCurrId: 0,
    // cateCurrIndex: 0,
    cateCurrName: '所有场景',
    dlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options, '123');
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    if (userInfo) {
      this.setData({
        isUser: true,
        isPhone: userInfo.phone ? true : false,
        userInfo: userInfo,
      });
    }
    this.typeList();
    this.getList(0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取用户信息
  getUserInfo: function (e) {
    var that = this;
    // // 获取用户code
    // wx.login({
    //   success: function (res) {
    //     // console.log(res);
    //     console.log('code:' + res.code);
    //     that.setData({
    //       jscode: res.code,
    //     });
    //     // 获取用户手机号
    //     that.getUserPhone({code: res.code});
    //   }
    // });
    // 获取用户信息
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        // console.log(res.userInfo);
        that.setData({
          userInfo: res.userInfo,
        });
        this.setWxInfo(res.userInfo);
      },
      fail: (res) => {
        console.log('error');
      }
    });
  },

  // 更新用户信息
  setWxInfo: function (e) {
    var that = this;
    var formData = {
      nickName: e.nickName,
      avatarUrl: e.avatarUrl,
      gender: e.gender,
      jscode: that.data.jscode,
      province: e.province,
      city: e.city,
      country: e.country,
      language: e.language,
    };
    console.log(formData);
    // console.log(app.globalData.domain);
    // return
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/user/auth',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        var userInfo = {
          wxid: res.data.wxid,
          phone: res.data.phone,
          nickName: e.nickName,
          avatarUrl: e.avatarUrl,
        };
        wx.setStorageSync('userInfo', userInfo);
        that.setData({
          isUser: true,
        });
      },
      fail: function () {
        console.log('网络错误');
      }
    });
  },

  // 输入手机号
  getPhone: function (e) {
    console.log(e);
    var that = this;
    var phone = e.detail.value;
    that.setData({
      userPhone: phone,
    });
  },

  // 设置手机号
  setPhone: function (e) {
    // console.log(e, this.data.userPhone);
    var that = this;
    var uinfo = this.data.userInfo;
    var userInfo = wx.getStorageSync('userInfo');
    var phone = this.data.userPhone;
    var formData = {
      wxid: uinfo.wxid,
      phone: phone,
    };
    console.log(formData);
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/user/tophone',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        userInfo.phone = uinfo.phone = phone;
        wx.setStorageSync('userInfo', userInfo);
        that.setData({
          isPhone: true,
          userPhone: phone,
          userInfo: uinfo,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // // 用户手机号授权
  // getPhoneNumber: function (e) {
  //   console.log(e);
  // },

  // // 获取用户手机号
  // getUserPhone: function (e) {
  //   var that = this;
  //   var formData = {
  //     code: e.code,
  //   };
  //   console.log(formData); 
  //   // return
  //   wx.request({
  //     method: 'POST',
  //     url: app.globalData.domainXcx + '/user/phonebycode',
  //     data: formData,
  //     timeout: 10000, //接口超时10S
  //     success: function (res) {
  //       res = res.data;
  //       console.log(res);
  //       if (res.code!=0) {
  //         wx.showToast({
  //           title: res.msg, icon: 'none'
  //         }); return
  //       }
  //       that.setData({
  //         isPhone: true,
  //         userPhone: res.data,
  //       });
  //     },
  //     fail: function () {
  //       console.log('网络错误');
  //     }
  //   });
  // },

  // onChooseAvatar(e) {
  //   console.log(e);
  //   this.setData({
  //     avatarUrl: e.detail.avatarUrl,
  //     nickname: ''
  //   })
  // },
  
  // // 去扫码
  // toScan: function (e) {
  //   // 允许从相机和相册扫码
  //   wx.scanCode({
  //     success (res) {
  //       var str = res.result;
  //       str = str.split("&code=");
  //       str = str[1];
  //       // 请求落地页
  //       wx.request({
  //         url: app.globalData.domainXcx+'/xcx/ldy/'+str,
  //         method: "POST",
  //         header: {'content-type': 'application/x-www-form-urlencoded'},
  //         data: { code: str },
  //         success: function (res) {
  //           wx.hideLoading();
  //           if (res.data.code!=0) {
  //           }
  //         }
  //       });
  //     }
  //   });
  // },

  // 进入个人物品详情
  toTmp: function (e) {
    console.log(e);
    var code = e.currentTarget.dataset.code;
    var id = e.currentTarget.dataset.id;
    var param = '?code=' + code + '&id=' + id;
    wx.navigateTo({
      url: '/pages/index/tmp/index' + param,
    }); return
  },

  // 进入店铺详情
  toShop: function (e) {
    console.log(e);
    var code = e.currentTarget.dataset.code;
    var id = e.currentTarget.dataset.id;
    var param = '?code=' + code + '&id=' + id;
    wx.navigateTo({
      url: '/pages/index/shop/index' + param,
    }); return
  },

  // 进入商品详情
  toGoods: function (e) {
    console.log(e);
    var code = e.currentTarget.dataset.code;
    var id = e.currentTarget.dataset.id;
    var param = '?code=' + code + '&id=' + id;
    wx.navigateTo({
      url: '/pages/index/goods/index' + param,
    }); return
  },

  // 进入收纳详情
  toShouna: function (e) {
    console.log(e);
    var code = e.currentTarget.dataset.code;
    var id = e.currentTarget.dataset.id;
    var param = '?code=' + code + '&id=' + id;
    wx.navigateTo({
      url: '/pages/index/shouna/index' + param,
    }); return
  },

//   // 切换类型
//   toType: function (e) {
//     console.log(e);
//     var lists = this.data.catelist;
//     var cateid = this.data.cateCurrId;
//     var cateIndex = this.data.cateCurrIndex;
//     var ii = 0;
//     if (cateid == 3) {
//       ii = 0;
//     } else {
//       ii = cateIndex * 1 + 1;
//     }
//     // console.log(ii); return
//     var currid = lists[ii].id;
//     this.setData({
//       cateCurrId: currid,
//       cateCurrName: lists[ii].title,
//       cateCurrIndex: ii,
//       dlist: this.getList(cateid),
//     });
//     this.popClose();
//   },

  // 切换类型
  toType: function (e) {
    // console.log(e); return
    var cateid = e.currentTarget.dataset.cateid;
    var cateName = e.currentTarget.dataset.catename;
    this.setData({
      cateCurrId: cateid,
      cateCurrName: cateName,
      dlist: this.getList(cateid),
    });
    this.popClose();
  },

  // 弹窗
  popShow: function (e) {
    this.setData({ isPop: 0 });
  },

  // 关闭弹窗
  popClose: function (e) {
    this.setData({ isPop: 1 });
  },

  // 分类
  typeList: function () {
    var that = this;
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/index/option',
      data: {},
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        var type = res.data.type;
        that.setData({
          catelist: type,
          cateCurrId: type[0].id,
          cateCurrIndex: 0,
          cateCurrName: type[0].title,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 列表数据
  getList: function (e) {
    console.log(e);
    var type = e;
    // var ds = [
    //   {
    //     id: 1, type: 1, title: '个人宝贝',
    //     img: '/imgs/bai.png',
    //     visitNum: 10, visitTotal: 100,
    //     orderNum: 10, orderTotal: 100,
    //     addtime: '2024-05-10 10:10',
    //     code: '1111'
    //   },
    //   {
    //     id: 2, type: 2, title: '黄焖鸡米饭',
    //     img: '/imgs/bai.png',
    //     visitNum: 10, visitTotal: 100,
    //     orderNum: 10, orderTotal: 100,
    //     addtime: '2024-05-10 10:10',
    //     code: '2222'
    //   },
    //   {
    //     id: 3, type: 3, title: '肉丝面',
    //     img: '/imgs/bai.png',
    //     visitNum: 10, visitTotal: 100,
    //     orderNum: 10, orderTotal: 100,
    //     addtime: '2024-05-10 10:10',
    //     code: '3333'
    //   }
    // ];
    // var dnew = [];
    // for (var i = 0; i < ds.length; i ++) {
    //   if (type > 0 && ds[i].type == type) {
    //     dnew.push(ds[i]);
    //   }
    // }
    // if (dnew.length == 0) {
    //   dnew = ds;
    // }
    // this.setData({
    //   dlist: dnew,
    // });
    var that = this;
    var userInfo = wx.getStorageSync('userInfo');
    var formData = {
      phone: userInfo.phone,
      type: type,
    };
    console.log(formData);
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/index/list',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
            that.setData({
                dlist: [],
            });
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        that.setData({
          dlist: res.data,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },
});